<?php
class FormItemRender
{
	static public function getItemName($conf)
	{
		return "attr_" . $conf["id"];
	}
	static public function render($conf)
	{
		$renderMethod = $conf["form_item_type"];
		$required = isset($conf["validator_conf"]["rules"]["required"]) && $conf["validator_conf"]["rules"]["required"] ? "<em>*</em>" : ""; 
		echo "<div><label>$required" . $conf['caption'] . '</label>';
		self::$renderMethod($conf);
		echo !empty($conf["tips_for_writer"]) ? "<span class=\"tips\">（" . $conf["tips_for_writer"] . "）</span>" : "";
		echo "</div>\n";
	}

	static public function radio($conf)
	{
		foreach ($conf['options'] as $option)
		{
			$checked = $option['option_index'] == $conf['default_value'] ? ' checked="checked"' : '';
			echo '<input name="' . self::getItemName($conf) . '" type="radio" value="' . $option['option_index'] . '"' . $checked . '>' . $option['option_text'] . '  ';
		}
	}

	static public function select($conf)
	{
		echo '<select name="' . self::getItemName($conf) . '">';
		echo isset($conf["validator_conf"]["rules"]["required"]) && false == $conf["validator_conf"]["rules"]["required"] ? "<option></option>" : "";
		foreach ($conf['options'] as $option)
		{
			$checked = $option['option_index'] == $conf['default_value'] ? ' selected="selected"' : '';
			echo '<option value="' . $option['option_index'] . '"' . $checked . '>' . $option['option_text'] . "</option>";
		}
		echo '</select>';
	}

	static public function checkbox($conf)
	{
		foreach ($conf['options'] as $option)
		{
			$checked = is_array($conf['default_value']) && in_array($option['option_index'], $conf['default_value']) ? ' checked="checked"' : "";
			echo '<input name="' . self::getItemName($conf) . '[]" type="checkbox" value="' . $option['option_index'] . '"' . $checked . '>' . $option['option_text'] . "  ";
		}
	}

	static public function multiple_select($conf)
	{
		echo '<select name="' . self::getItemName($conf) . '[]" multiple="multiple">';
		foreach ($conf['options'] as $option)
		{
			$checked = is_array($conf['default_value']) && in_array($option['option_index'], $conf['default_value']) ? ' selected="selected"' : "";
			echo '<option value="' . $option['option_index'] . '"' . $checked . '>' . $option['option_text'] . "</option>";
		}
		echo '</select>';
	}

	static public function single_input($conf)
	{
		echo '<input name="' . self::getItemName($conf) . '" type="text" value="' . $conf['default_value'] . '">';
	}

	static public function textarea($conf)
	{
		echo '<textarea name="' . self::getItemName($conf) . '">' . $conf['default_value'] . '</textarea>';
	}
}